package com.ruoyi.ctcx.controller;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.ctcx.bo.FileUploadResBo;
import com.ruoyi.ctcx.service.FileUploadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.net.URLDecoder;
import java.util.Map;

@RestController
@RequestMapping("file")
public class FileUploadController extends BaseController {
    @Autowired
    private FileUploadService fileUploadService;

    @RequestMapping("upload")
    public AjaxResult uploadFile(MultipartFile file){
        String path = IdUtils.simpleUUID();
        FileUploadResBo resBo = fileUploadService.uploadFile(file,path);
        resBo.setUserName(getUsername());
        return success(resBo);
    }
    @RequestMapping("delFile")
    public AjaxResult delFile(@RequestBody Map<String,String> paramMap){
        String path = paramMap.get("path");
        fileUploadService.deleteFile(path);
        return success("删除成功");
    }

    @RequestMapping("uploadByContent")
    public String uploadByContent(String content,String fileType){
        String path = fileType;
        return fileUploadService.uploadByContent(URLDecoder.decode(content),path,fileType);
    }
}
